package com.qyhl.module_practice.newhome.fragment.act;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeAcitivityBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PracticeHomeActFragment extends BaseFragment {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;

    private View view;

    private EmptyWrapper<PracticeAcitivityBean> emptyWrapper;

    private List<PracticeAcitivityBean> mDataList = new ArrayList<>();

    private String instId;


    public void setInstId(String instId) {
        this.instId = instId;
    }

    private CommonAdapter<PracticeAcitivityBean> commonAdapter;

    public void setDataList(List<PracticeAcitivityBean> mDataList) {
        this.mDataList = mDataList;
    }

    public static PracticeHomeActFragment newInstance(List<PracticeAcitivityBean> beanList, String instId) {
        PracticeHomeActFragment fragment = new PracticeHomeActFragment();
        fragment.setDataList(beanList);
        fragment.setInstId(instId);
        return fragment;
    }


    @Override
    public void initImmersionBar() {

    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.practice_fragment_home_act, null);
    }

    @Override
    protected void create() {
        BusFactory.getBus().register(this);
        loadData();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        initView();

    }
    private void initView() {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeAcitivityBean>(getContext(), R.layout.practice_item_home_act, mDataList) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void convert(ViewHolder holder, PracticeAcitivityBean bean, int position) {
                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(getContext().getApplicationContext())
                        .load(bean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                        .into(cover);

                TextView title = holder.getView(R.id.title);

                TextView endTime = holder.getView(R.id.end_time);
                TextView personTxt = holder.getView(R.id.person_txt);
                TextView personNum = holder.getView(R.id.person_num);

                ImageView status = holder.getView(R.id.status);

                title.setText(bean.getName());
                endTime.setText("招募截止：" + DateUtils.practiceRecFormat(bean.getEndTime()));
                if (bean.getQuantityDemanded() == 0) {
                    personNum.setText(bean.getVolunteerNum() + "/不限");
                } else {
                    personNum.setText(bean.getVolunteerNum() + "/" + bean.getQuantityDemanded());
                }

                switch (bean.getStatus()) {
                    case "RECRUIT":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_join_icon)
                                .into(status);
                        break;
                    case "SERVICE":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_service_icon)
                                .into(status);
                        break;
                    case "BEGINNING":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_on_icon)
                                .into(status);

                        break;
                    case "END":
                        title.setTextColor(getResources().getColor(R.color.practice_global_black_lv1_60per));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));
                        personNum.setTextColor(getResources().getColor(R.color.practice_normal_color_60per));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_end_icon)
                                .into(status);
                        break;
                    case "NOT_BEGIN":
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_forecast_icon)
                                .into(status);
                        break;
                    default:
                        title.setTextColor(getResources().getColor(R.color.global_black_lv1));
                        endTime.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personTxt.setTextColor(getResources().getColor(R.color.practice_normal_color));
                        personNum.setTextColor(getResources().getColor(R.color.global_base));

                        Glide.with(getContext().getApplicationContext())
                                .load(R.drawable.practice_act_on_icon)
                                .into(status);
                        break;
                }
            }
        });

        emptyWrapper = new EmptyWrapper<>(commonAdapter);
        emptyWrapper.setEmptyView(R.layout.practice_layout_list_empty_view);
        recycleView.setAdapter(emptyWrapper);
    }

    private void setListeners() {
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                String userName = CommonUtils.getInstance().getUserName();

                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                bundle.putString("userName", userName);
                bundle.putString("instId", instId);
                bundle.putString("title", mDataList.get(position).getName());

                RouterManager.router(ARouterPathConstant.PRACTICE_ACT_DETAIL,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Event.PracticeHomeActRefresh event) {
        if (event != null) {
            List<PracticeAcitivityBean> list = event.getBeanList();
            if (list != null && list.size() > 0) {
                mDataList.clear();
                mDataList.addAll(list);
                emptyWrapper.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }


}
